﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Utils
{
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Controllers;
    using Microsoft.AspNetCore.Mvc.Routing;
    using System.Reflection;

    public class ApiScanner
    {
        //public static IEnumerable<ApiPermission> GetAllApiEndpoints(Assembly assembly)
        //{
        //    var controllers = assembly.GetTypes()
        //        .Where(type => type.IsSubclassOf(typeof(ControllerBase)));

        //    var apiEndpoints = new List<ApiPermission>();

        //    foreach (var controller in controllers)
        //    {
        //        var controllerRoute = controller.GetCustomAttribute<RouteAttribute>()?.Template;

        //        var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance)
        //            .Where(m => m.IsDefined(typeof(HttpMethodAttribute)));

        //        foreach (var method in methods)
        //        {
        //            var httpMethodAttr = method.GetCustomAttributes<HttpMethodAttribute>().FirstOrDefault();
        //            if (httpMethodAttr == null) continue;

        //            var actionRoute = httpMethodAttr.Template ?? string.Empty;

        //            // 拼接完整路径
        //            var fullPath = CombineRoutes(controllerRoute, actionRoute);

        //            apiEndpoints.Add(new ApiPermission
        //            {
        //                Path = fullPath,
        //                HttpMethod = httpMethodAttr.HttpMethods.FirstOrDefault() ?? "GET",
        //                PermissionKey = null, // 默认未设置权限字符串
        //                IsEnabled = true // 默认启用
        //            });
        //        }
        //    }

        //    return apiEndpoints;
        //}

        //private static string CombineRoutes(string? controllerRoute, string actionRoute)
        //{
        //    var baseRoute = controllerRoute?.TrimEnd('/') ?? string.Empty;
        //    var actionPart = actionRoute.TrimStart('/');
        //    return $"{baseRoute}/{actionPart}".Trim('/');
        //}
    }

}
